package com.sqlcheck.entity;

import com.baomidou.mybatisplus.annotation.*;
import com.sqlcheck.common.enums.DatabaseType;
import com.sqlcheck.util.JsonUtils;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * 提示词模板实体
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@TableName(value = "llm_prompt_templates")
public class LLMPromptTemplate {

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    @TableField("template_key")
    private String templateKey;

    @TableField("template_name")
    private String templateName;

    @TableField("category")
    private String category;

    @TableField("source_db_type")
    private DatabaseType sourceDbType;

    @TableField("target_db_type")
    private DatabaseType targetDbType;

    @TableField("sql_types")
    private String sqlTypesJson; // JSON格式存储

    @TableField("template_content")
    private String templateContent;

    @TableField("variables")
    private String variablesJson; // JSON格式存储

    @TableField("example_usage")
    private String exampleUsage;

    @TableField("description")
    private String description;

    @TableField("instructions")
    private String instructions;

    @TableField("expected_output_format")
    private String expectedOutputFormat;

    @TableField("version")
    private String version;

    @TableField("is_active")
    private Boolean isActive;

    @TableField("is_system")
    private Boolean isSystem;

    @TableField("priority")
    private Integer priority;

    @TableField("usage_count")
    private Integer usageCount;

    @TableField("success_rate")
    private BigDecimal successRate;

    @TableField("success_count")
    private Integer successCount;

    @TableField("last_used_at")
    private LocalDateTime lastUsedAt;

    @TableField("average_processing_time")
    private Integer averageProcessingTime;

    @TableField("enabled")
    private Boolean enabled;

    @TableField(value = "created_at", fill = FieldFill.INSERT)
    private LocalDateTime createdAt;

    @TableField(value = "updated_at", fill = FieldFill.INSERT_UPDATE)
    private LocalDateTime updatedAt;

    @TableField("created_by")
    private Long createdBy;

    @TableField("updated_by")
    private Long updatedBy;

    @TableLogic
    @TableField("deleted")
    private Boolean deleted;

    // Helper methods for JSON fields
    public List<String> getSqlTypes() {
        return JsonUtils.jsonToList(this.sqlTypesJson, String.class);
    }

    public LLMPromptTemplate setSqlTypes(List<String> sqlTypes) {
        this.sqlTypesJson = JsonUtils.listToJson(sqlTypes);
        return this;
    }

    public Map<String, Object> getVariables() {
        return JsonUtils.safeParseMap(this.variablesJson);
    }

    public LLMPromptTemplate setVariables(Map<String, Object> variables) {
        this.variablesJson = JsonUtils.safeToJsonString(variables);
        return this;
    }

    // Alias method for templateContent
    public String getTemplate() {
        return this.templateContent;
    }

    public LLMPromptTemplate setTemplate(String template) {
        this.templateContent = template;
        return this;
    }
}